<template>
	<om-addpage :saveBtnText="'确定'" :showHistoryBtn="false" :busCode="busCode" :menuName="menuName"
		@formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
		<uni-list>
			<uni-list-item :showArrow="false" :showHover="false">
				<radio-group @change="radioChange">
					<view style="display: flex; flex-flow:row wrap">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in checkRadio"
							:key="item.value">
							<view class="tui-flex" style="padding-right: 5px;padding-top: 5px;">
								<view>
									<radio :value="item.value" :checked="index === radioIndex" />
								</view>
								<view>{{ item.text }}</view>
							</view>
						</label>
					</view>
				</radio-group>
			</uni-list-item>
			<uni-list-item v-if="showChangeNode && isApprove=='1'" :showHover="false" :showArrow="changeNode">
				<view class="tui-flex align-center">
					<view class="tui-flex tui-col-6">
						<om-label label="转办" :required="changeNodeRequired"></om-label>
						<switch @change="switchChangeNodeChange" :class="changeNode ? 'checked' : ''"
							:checked="changeNode ? true : false"></switch>
					</view>
					<view v-if="changeNode" class="tui-right tui-col-6" @click="linkChangeNodeUserFunc">
						{{ changeNodeUserNames || '请选择' }}</view>
				</view>
			</uni-list-item>
			<uni-list-item v-if="showChangeApprover && isApprove=='1'" :showHover="false" :showArrow="changeApprover">
				<view class="tui-flex align-center">
					<view class="tui-col-6">
						更换审批人
						<switch @change="switchApproverChange" :class="changeApprover ? 'checked' : ''"
							:checked="changeApprover ? true : false"></switch>
					</view>
					<view v-if="changeApprover" class="tui-right tui-col-6" @click="linkApproveUserFunc">
						{{ approveUserName || '请选择' }}</view>
				</view>
			</uni-list-item>
			<uni-list-item v-if="isApprove=='4'" :showHover="false" :showArrow="false">
				<view class="tui-left" style="text-align: left;">
					{{'驳回方式'}}
				</view>
				<view class="tui-flex align-center">
					<radio-group @change="backTypeRadioChange">
						<label class="uni-list-cell uni-list-cell-pd"
							style="boorder: 1px solid red; display: flex;margin-top: 5px;"
							v-for="(item, index) in backTypeList" :key="item.value">
							<view>
								<radio :value="item.value" :checked="index === current" />
							</view>
							<view>{{item.name}}</view>
						</label>
					</radio-group>
				</view>
			</uni-list-item>
			<uni-list-item v-if="isApprove=='4'" :showHover="false">
				<view class="tui-flex align-center">
					<view class="tui-col-6">
						驳回节点
					</view>
					<view class="tui-right tui-col-6" @click="linkBackFlowNodeFunc">
						{{backFlowNodeName||"请选择"}}
					</view>
				</view>
			</uni-list-item>
			<uni-list-item :showArrow="false" :showHover="false">
				<view style="text-align: left;"><textarea :auto-height="true" style="line-height: 16px;"
						class="om-textarea" v-model="opinion" placeholder="意见说明……" /></view>
			</uni-list-item>
		</uni-list>
	</om-addpage>
</template>

<script>
	import {
		buttonShow
	} from '@/static/js/buttonShow.js';
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				backFlowNodeCache: "",
				buttons: [],
				infonameId: "",
				backFlowNodeName: '',
				approveUserId: '',
				radioIndex: 0,
				busCode: "",
				fileId: "",
				checkedit:true,
				fileCode: '',
				backNodeId: '',
				backUserId: '',
				checkRadio: [],
				showChangeNode: false, //显示转办
				showChangeApprover: false, //显示更换审批人
				isApprove: '1', //用户勾选
				opinion: '', //审核意见，
				backTypeList: [{
					value: '1',
					name: '接收节点处理后，按顺序流转'
				}, {
					value: '2',
					name: '接收节点处理后，直接返给我'
				}],
				backType: '',
				current: 0,
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			backTypeRadioChange(evt) {
				let _that = this
				for (let i = 0; i < _that.backTypeList.length; i++) {
					if (_that.backTypeList[i].value === evt.detail.value) {
						_that.current = i;
						_that.backType = evt.detail.value
						break;
					}
				}
			},
			formSubmit() {
				let _that = this
				let obj = {
					formId: _that.fileId,
					clazzName: 'com.room.hr.model.TlApproval',
					passOrFail: '',
					isApprove: _that.isApprove,
					approveUserId: '',
					opinion: _that.opinion,
					backNodeId: _that.backNodeId,
					backUserId: _that.backUserId,
					businessFormCode: _that.busCode,
					fileOrgId: 1,
					PAGE_CODE: _that.busCode,
					FILE_CODE: _that.fileCode,
					objStr: _that.formdata.objStr,
					instDirectorStr: _that.formdata.instDirectorStr || [],
					businessDirectorStr: _that.formdata.businessDirectorStr || [],
					backType: _that.backType
				}
				_that.om.request(
					"tlApproval/doAudited",
					obj,
					'post'
				).then(res => {
					if (res.succeed) {
						_that.om.getFormInfo(_that.fileId, _that.busCode, true)
					} else {
						_that.om.toast(res.msg)
					}
				})
			},
			linkBackFlowNodeFunc() {
				let pages = getCurrentPages();
				let page = pages[pages.length - 2];
				if (this.isApprove === "4") {
					this.backFlowNodeCache = page.backFlowNodeCache
					let url = getUrl('/pages/public/search/search', {
						other: JSON.stringify({
							businessFormCode: this.busCode,
							formId: this.fileId
						}),
						namefield: 'role|sort',
						namesplit: '|',
						extrafield: 'userNames',
						url: 'common/getBackFlowList',
						cachename: page.backFlowNodeCache,
						placeholder: '请选择',
						initload: true,
						searchbar: false
					});
					uni.navigateTo({
						url: url
					});
				}
			},
			radioChange(e) {
				this.isApprove = e.target.value;
				for (let i = 0; i < this.checkRadio.length; i++) {
					if (this.checkRadio[i].value === e.target.value) {
						this.radioIndex = i;
						break;
					}
				}
				if (this.isApprove !== "1") {
					this.changeApprover = false
					this.approveUserId = ""
					this.approveUserName = ""
				}
				if (this.isApprove !== "4") {
					this.backNodeIndex = 0
				}
			},
			getCheckRadio(btnArr) {
				let _that = this;
				this.checkRadio = btnArr;
			},
			getWorkParam() {
				let _that = this
				return {
					infonameId: _that.infonameId,
					showDel: "false",
					isCheck: 'true'
				}
			},
			getWorkParam1() {
				let _that = this
				return {
					isCheck: 'true'
				}
			},
			initLoad(options) {
				let _that = this
				let ajaxArr = [{
					url: 'tlApproval/getInfo',
					param: {
						id: options.fileId
					}
				}, {
					url: "tlApproval/getAuditedAuth",
					param: {
						formId: _that.fileId
					},
					method: "post"
				}]
				formModelLoad(ajaxArr, function(res, mainData) {
					_that.directorFlag = res[1].value.directorFlag
					_that.handOverFlag = res[1].value.handOverFlag

					_that.infonameId = res[0].value.tlApproval.infonameId
					if (_that.subEditorObj['handleInfo']) {
						_that.subEditorObj['handleInfo'].workList = res[0].value.tlApproval_handles.filter(e => e
							.handleUserId == _that.userId)
					}
					if (_that.subEditorObj['instDirectorInfo']) {
						_that.subEditorObj['instDirectorInfo'].workList = res[0].value.instDirectorList.filter(e =>
							e.instDirectorId == _that.userId)
					}
					if (_that.subEditorObj['businessDirectorInfo']) {
						_that.subEditorObj['businessDirectorInfo'].workList = res[0].value.businessDirectorList.filter(e => e.businessDirectorId == _that.userId)
					}
					_that.fileCode = res[0].value.tlApproval.code
					changeConfigAttr('topinfo', 'editors', [], mainData)
					for (let item of res[res.length - 1].value.bodyTitle) {
						if (item.fieldId && item.fieldId != 'handleInfo' && item.fieldId != "instDirectorInfo" &&
							item.fieldId != "businessDirectorInfo") {
							changeConfigAttr(item.fieldId, 'editors', [], mainData)
						} else {
							if (item.fieldId == 'handleInfo') {
								changeEditor("handleInfo", "objStr", 'showBtn', true, mainData)
								changeEditor("handleInfo", "objStr", 'btnText', true, mainData)
								if (_that.subEditorObj['handleInfo'].workList && _that.subEditorObj['handleInfo']
									.workList.length == 0) {
									changeConfigAttr(item.fieldId, 'editors', [], mainData)
								}
								if (!_that.handOverFlag) {
									changeConfigAttr(item.fieldId, 'editors', [], mainData)
								}
							}
							if (item.fieldId == 'instDirectorInfo') {
								let iIdList = res[0].value.instDirectorList.map(e => e.instDirectorId)
								let resultI = iIdList.findIndex(r =>{
								    return r == _that.userId
								});
								if (resultI == -1) {
									changeConfigAttr(item.fieldId, 'editors', [], mainData)
								}
								if (!_that.directorFlag) {
									changeConfigAttr(item.fieldId, 'editors', [], mainData)
								}
							}
							if (item.fieldId == 'businessDirectorInfo') {
								let bIdList = res[0].value.businessDirectorList.map(e => e.businessDirectorId)
								let resultB = bIdList.findIndex(r =>{
									return r == _that.userId
								});
								if (resultB == -1) {
									changeConfigAttr(item.fieldId, 'editors', [], mainData)
								}
							}
						}
					}
					if (_that.subEditorObj["handleInfo"]) {
						let att = {
							getWorkParam: "getWorkParam",
							showAdd: false,
							showDel: false
						}
						let configData = _that.subConfigData["handleInfo"]
						if (hasEditor("handleInfo", "itemId", configData)) {
							att.titleTemplate = "<%= itemName %>"
						}
						if (hasEditor("handleInfo", "checkFlagName", configData)) {
							att.subtitle1Template = "<%= checkFlagName?'已交接':'未交接' %>"
						}
						if (hasEditor("handleInfo", "handleUserRemark", configData)) {
							let title = getEditorTitle("handleInfo", 'handleUserRemark', configData)
							att.subtitle2Template = title + ":<%= handleUserRemark || '未填写' %>"
						}
						changeEditor("handleInfo", "objStr", att, mainData)
					}
					if (_that.subEditorObj["instDirectorInfo"]) {
						let att = {
							showAdd: false,
							showDel: false
						}
						let configData = _that.subConfigData["instDirectorInfo"]
						if (hasEditor("instDirectorInfo", "instDirectorName", configData)) {
							att.titleTemplate = "<%= instDirectorName %>"
						}
						if (hasEditor("instDirectorInfo", "interviewFlag", configData)) {
							att.subtitle1Template = "<%= interviewFlag?'已面谈':'未面谈'%>"
						}
						changeEditor("instDirectorInfo", "instDirectorStr", att, mainData)
					}
					if (_that.subEditorObj["businessDirectorInfo"]) {
						let att = {
							showAdd: false,
							showDel: false,
							getWorkParam: "getWorkParam1"
						}
						let configData = _that.subConfigData["businessDirectorInfo"]
						if (hasEditor("businessDirectorInfo", "businessDirectorId", configData)) {
							att.titleTemplate = "<%= businessDirectorName %>"
						}
						if (hasEditor("businessDirectorInfo", "interviewFlag", configData)) {
							att.subtitle1Template = "<%= interviewFlag?'已面谈':'未面谈'%>"
						}
						// if (hasEditor("businessDirectorInfo", "interviewFlag", configData)) {
						// 	att.subtitle2Template = "<%= %>"
						// }
						changeEditor("businessDirectorInfo", "businessDirectorStr", att, mainData)
					}
				}, options)
			}
		},
		onLoad(options) {
			let _that = this
			this.busCode = options.code
			this.fileId = options.fileId
			_that.getCheckRadio(JSON.parse(options.btnArr))
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				for (let fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key: _that.backFlowNodeCache,
					success: (res) => {
						if (res.data) {
							let d = res.data
							this.backFlowNodeName = d.roleSort
							if (d.userList) {
								this.backUserId = d.userList.map(e => e.id).join(',')
							}
							this.backNodeId = d.nodeId
							uni.removeStorage({
								key: _that.backFlowNodeCache
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>
